package cn.kami.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kami.dao.IStockIn;
import cn.kami.vo.StockIn;

public class StockInDAOImpl implements IStockIn {
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	public StockInDAOImpl(Connection conn){
		this.conn = conn;
	}
	@Override
	public boolean doCreate(StockIn si) throws Exception {
		// TODO Auto-generated method stub
		boolean flag = false;
		String sql = "INSERT INTO stockin(si_id,sier_name,remark,bs_id,in_time)VALUES(?,?,?,?,?)";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1,si.getSiID());
		this.pstmt.setString(2,si.getSierName());
		this.pstmt.setString(3,si.getRemark());
		this.pstmt.setString(4,si.getBsID());
		this.pstmt.setDate(5,new java.sql.Date(si.getInTime().getTime()));
		if(this.pstmt.executeUpdate()>0){
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}
	@Override
	public String getSiID() throws Exception {
		String siID = null;
		int count = 0;
		String sql = "SELECT COUNT(si_id) FROM stockin";
		this.pstmt = this.conn.prepareStatement(sql);
		ResultSet rs = this.pstmt.executeQuery();
		while(rs.next()){
			count = rs.getInt(1);
		}
		this.pstmt.close();
		siID = "si" + (count+1);
		return siID;
	}
	@Override
	public List<StockIn> findByName(String name) throws Exception {
		// TODO Auto-generated method stub
		ArrayList<StockIn> all = new ArrayList<StockIn>();
		String sql = "SELECT si_id,sier_name,remark,bs_id,in_time FROM StockIn WHERE sier_name=?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setString(1,name);
		ResultSet rs = this.pstmt.executeQuery();
		while(rs.next()){
			StockIn si = new StockIn();
			si.setBsID(rs.getString(1));
			si.setSierName(rs.getString(2));
			si.setRemark(rs.getString(3));
			si.setBsID(rs.getString(4));
			si.setInTime(rs.getDate(5));
			all.add(si);
		}
		this.pstmt.close();
		return all;
	}

}
